import axios from "axios";
import qs from "qs";
import config from "./config";
let {host,appKey,accessToken} = config;


//参赛团
//{{url}}/api/Personal/v200/User/GetEventGroup?accessToken={{accessToken}}&appKey={{appKey}}
export function getGroup(token){
   return axios.get(`${config.host}/Personal/v200/User/GetEventGroup?accessToken=${token}&appKey=${config.appKey}`);
}
                             
export function serverTime(){
  return axios.get(`https://game.jixiucloud.cn/api/CampaignGames/TimeStamp`);
}
//我的帖子列表接口
export function getAllPostList(skip,AppUserID){
   return axios.get(`${config.host}/Personal/v200/post/getvotedposts?take=10&skip=${skip}&appKey=${config.appKey}&accessToken=${config.accessToken}&categoryId=15946&sortType=0&isGetChildCategory=true&targetAppUserID=${AppUserID}`)
}


//首页列表接口
export function getIndexList(skip,categoryId){
    return axios.get(`${config.host}/Personal/v200/post/getvotedposts?take=15&skip=${skip}&appKey=${config.appKey}&accessToken=${config.accessToken}&categoryId=${categoryId}&sortType=19&isGetChildCategory=true`)
}

//首页列表接口

export function getMyPostlist(skip,categoryId,targetAppUserID,token){
    return axios.get(`${config.host}/Personal/v200/Post/getvotedposts?appKey=${config.appKey}&accessToken=${token}&take=15&skip=${skip}&categoryId=${categoryId}&isGetChildCategory=true&sortType=0&targetAppUserID=${targetAppUserID}`);
}
//删除帖子
export function DeletePost(token,postId){
  return axios.delete(`${config.host}/Personal/v200/Post/Post?appKey=${config.appKey}&appAccessToken=${token}&postId=${postId}`);
}
//分享
export function WbShares(token,postId){
  return axios.get(`${config.host}/Personal/v200/Post/Share?appKey=${config.appKey}&accessToken=${token}&postId=${postId}&socialPlatformId=4`);
}

//短链接
export function shorturl(url){
  return axios.get(`https://game.jixiucloud.cn/api/CampaignGameUsers/ShortUrl?url=${url}`);
}
//单个用户帖子列表
// export function getMyPostlist(skip,categoryId,targetAppUserID,token){
//     return axios.get(`${config.host}/Personal/v200/Post/MyPostList?appKey=${config.appKey}&accessToken=${token}&take=15&skip=${skip}&categoryId=${categoryId}&isGetChildCategory=true&sortType=0&targetAppUserID=${targetAppUserID}`);
// }




//作品分享接口
export function sharePost(token,postId){
  return axios.get(`${config.host}/Personal/v200/Post/Share?appKey=${config.appKey}&accessToken=${token}&postId=${postId}&socialPlatformId=4`);
}




//所有用户帖子列表
export function getPostlist(skip,appkey){
    return axios.get(`${config.host}/Personal/v200/post/getposts?appKey=${appkey}&accessToken=${accessToken}&categoryId=15411&take=15&skip=${skip}&ismappedmenu=true`)
}


//获取用户信息
export function getAppUser(token){
    return axios.get(`${config.host}/Core/v200/appUser/Get?appKey=${config.appKey}&accessToken=${token}`);  
}



//登录	
export function login(data){
  return axios.post(`${config.host}/Core/v200/AppUser/Login/?appKey=${appKey}`,qs.stringify(data));
}


//第三方登录
export function authLogin(data){
  return axios.post(`${config.host}/Core/v200/appUser/LoginOAuth?appKey=${config.appKey}`,qs.stringify(data));
}

//注册
export function register(data){
  return axios.post(`${config.host}/Core/v200/appUser/RegisterPhone?appKey=${config.appKey}`,qs.stringify(data))
}

//任务列表
export function tasklist(token){
  return axios.get(`${config.host}/Personal/v200/Group/User/Tasks?appKey=${config.appKey}&accessToken=${token}&isNewTask=true`)
}


//金币数
export function goldCoin(token){
  return axios.get(`${config.host}/Core/v200/appUser/Get?appKey=${config.appKey}&accessToken=${token}`)
}

//今日已领金币数
export function rewardPoints(token){
  return axios.get(`${config.host}/Personal/v200/Reward/Points?appKey=${config.appKey}&accessToken=${token}&priceType=2`)
}

//判断是否已签到
export function Checkin(token){
   return axios.get(`${config.host}/Personal/v200/Checkin/MyConsecutiveList?appKey=${config.appKey}&accessToken=${token}`)
}

//提交签到

export function CheckinPost(token){
  return axios.post(`${config.host}/Personal/v200/Checkin/Post?appKey=${config.appKey}&accessToken=${token}`)
}


//初始化获取token接口
 export function getToken(){
  //return axios.post(`${config.host}/Core/v200/Device?appKey=qNWmtuX04JGyG-vNLK84V6uUMNPY8lZHR473dXAWiiy9ZT7oOrxxJ2MUCzeaOwzTZH8paczG1O6gzkIYUDa9Ug`,qs.stringify({"DeviceType":6,"DeviceId":"163170"}));
  return axios.post(`${config.host}/Core/v200/Device?appKey=${config.appKey}`,qs.stringify({"DeviceType":6,"DeviceId":new Date().getTime().toString()}));
 }



export function getIndexlist(skip,token,sortType){
  return axios.get(config.host+'/Personal/v200/post/getvotedposts?appKey='+config.appKey+'&accessToken='+token+'&categoryId='+config.CategoryId+'&skip='+skip+'&take=10&sortType='+sortType);
}

export function postVote(postId,voteQuantity,accessToken){
  return axios.post(`${config.host}/Personal/v200/Post/VotePost?appKey=${config.appKey}&accessToken=${accessToken}&postId=${postId}&voteQuantity=${voteQuantity}`);
}

export function postContribute(accessToken){
  let body ={"Type":1,"Title":"2222","Html":"","IsPublished":true,"Status":0,"CategoryId":7513,"SubPostList":[{"TempPostId":"temp0","Type":255,"Title":"","Html":"","Status":0,"CategoryId":7513,"SubPostList":[],"TaggedUser":{},"TaggedUsers":[],"StreamVideoList":[],"PostInfo":{"BadgeImage":null,"IsForcedShare":"0","AlbumInfo":{}},"ActivationCount":{},"TranslatedAlbumInfo":[],"PhotoList":[{"Type":0}],"VideoList":[],"TravelProduct":{},"CreateDate":"2019-11-14T03:09:32.374Z"}],"TaggedUser":{},"TaggedUsers":[],"StreamVideoList":[],"PostInfo":{"BadgeImage":null,"IsForcedShare":"0","AlbumInfo":{},"EventGoinCoinReward":null},"ActivationCount":{"LikeCount":99,"ShareCount":52,"ViewCount":71,"RangeId":635,"IsSet":true},"TagList":[],"TranslatedAlbumInfo":[],"SourceLang":2,"SourceLangDesc":2,"PhotoList":[],"VideoList":[],"TravelProduct":{},"LiveStream":{"NotificationTime":[],"IsPublished":true},"Chatrooms":[{"BackgroundImageUrl":null}]}
  return axios.post(`http://jixiuapp-staging.chinacloudapp.cn/V2/Content/AddPost?appKey=${config.appKey}&accessToken=${accessToken}`,JSON.stringify(body));
}
//艺人 – 贴吧 – 发贴
export function starPostTieba(body,accessToken){
  return axios.post(`${host}/Personal/v200/Post?appKey=${appKey}&appAccessToken=${accessToken}`,
  body
  );
}
//艺人 – 贴吧 – 发贴 - 图片
export function starPostImg(params,photo,accessToken){
  return axios.post(`${host}/Personal/v200/Post/SubPhoto?appKey=${appKey}&appAccessToken=${accessToken}&${qs.stringify(params)}`,
  photo
  );
}
//艺人 – 贴吧 – 发贴 - 视频
export function starPostVideo(params,appKey,video,accessToken){
  return axios.post(`${host}/Personal/v200/Post/SubVideo?appKey=${appKey}&appAccessToken=${accessToken}&${qs.stringify(params)}`,
  video
  );
}
//艺人 – 粉丝团 – 发贴
export function starGroupPost(appKey,body,accessToken){
  return axios.post(`${host}/Personal/v200/Post?appKey=${appKey}&appAccessToken=${accessToken}`,
  body
  );
}
//发送验证码
export function postsendTac(phoneNumber){
  return axios.get(`${host}/Core/v200/AppUser/TAC?appKey=${appKey}&phoneNumber=${phoneNumber}&tacType=1&phoneCountryCode=86`
  );
}
//验证验证码
export function yzTac(phoneNumber,tac,messageId){
  return axios.get(`${host}/Core/v200/AppUser/VerifyTAC?appKey=${appKey}&phoneNumber=${phoneNumber}&messageId=${messageId}&tac=${tac}&phoneCountryCode=86`
  );
}

//更新个人信息
export function UpdateUser(accessToken,body){
  return axios.post(`${host}/Core/v200/AppUser/Update?appKey=${appKey}&accessToken=${accessToken}`,body
  );
}














//星讯 - 详情
export function newsDetail(params,accessToken){
    return axios.get(`${host}/Personal/v200/Post/Get?appKey=${appKey}&accessToken=${accessToken}&${qs.stringify(params)}`);
}
//星讯 - 评论 - 列表
export function comments(params,accessToken){
  return axios.get(`${host}/Personal/v200/Post/getcomments?appKey=${appKey}&accessToken=${accessToken}&${qs.stringify(params)}`);
}
//星讯 - 评论 - 提交
export function commentPost(params,msg,accessToken){
  return axios.post(`${host}/Personal/v200/Post/createcomment?appKey=${appKey}&accessToken=${accessToken}&${qs.stringify(params)}`,{'Text':msg})
}
//星讯 - 详情 - 点赞
export function postLike(params,appKey,accessToken){
  //let appKey = "lOaOWr2Kp37mjpugI5PkjbI-kCyWFXJr2aQot_CUBAKzCwqUgrNctNY5vY88VcyiBUazexDrG11FzEdk822pXw"
  return axios.post(`${host}/Personal/v200/Post/Likepost?appKey=${appKey}&accessToken=${accessToken}&${qs.stringify(params)}`);
}
//星讯 - 详情 - 取消点赞
export function postunLike(params,appKey,accessToken){
  //let appKey = "lOaOWr2Kp37mjpugI5PkjbI-kCyWFXJr2aQot_CUBAKzCwqUgrNctNY5vY88VcyiBUazexDrG11FzEdk822pXw"
  return axios.post(`${host}/Personal/v200/Post/Unlike?appKey=${appKey}&accessToken=${accessToken}&${qs.stringify(params)}`);
}

export function updata(params,appKey,accessToken){
  //let appKey = "lOaOWr2Kp37mjpugI5PkjbI-kCyWFXJr2aQot_CUBAKzCwqUgrNctNY5vY88VcyiBUazexDrG11FzEdk822pXw"
  return axios.post(`${host}/Personal/v200/Post/Unlike?appKey=${appKey}&accessToken=${accessToken}&${qs.stringify(params)}`);
}


var Ajaxshare = {
    
    get: function(url,fn) {
      // XMLHttpRequest对象用于在后台与服务器交换数据   
      var xhr = new XMLHttpRequest();            
      xhr.open('GET', url, false);
      xhr.setRequestHeader("Ocp-Apim-Subscription-Key", '35a0672e5ff94b72a2e658e3debb2237')
        // beforeSend: function beforeSend(xhrObj) {
        //     xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key", '35a0672e5ff94b72a2e658e3debb2237');
        // }
      xhr.onreadystatechange = function() {
        // readyState == 4说明请求已完成
        if(xhr.readyState === 4){
            if ( xhr.status === 200 || xhr.status === 304) { 
              // 从服务器获得数据 
              fn.call(this, xhr.responseText);  
            }
        }
       
      };
      xhr.send();
    },
  }
  function IsWechat(){
    var ua = navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) === "micromessenger") {
        return true;
    }
    return false;
};
export function ShareInit(sharelink,sharetitle,shareimg){
  let url = `https://openapis.ixingban.com/gamev3/api/Wechat/CustomShare?appTag=1&url=${encodeURIComponent(window.location.href)}`
  if (!IsWechat()) {
   return;
  }
  Ajaxshare.get(url,(res)=>{
     let data = JSON.parse(res)
      if (!data || data.errmsg) {
          throw data;
      }

      window.wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
          appId: data.appId, // 必填，公众号的唯一标识
          timestamp: data.timeStamp, // 必填，生成签名的时间戳
          nonceStr: data.nonceStr, // 必填，生成签名的随机串
          signature: data.signature.toLowerCase(), // 必填，签名
          jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareQZone', 'onMenuShareWeibo']
      });
  })
 
  window.wx.ready(function () {
  var shareObj = {
      title: sharetitle, // 分享标题  
      desc: '来自星伴网的分享', // 分享描述  
      link: sharelink, // 分享链接
      imgUrl: shareimg, // 分享图标  
      success: function success() {

      },
      cancel: function cancel() {

      }
  };

  window.wx.onMenuShareTimeline(shareObj);
  window.wx.onMenuShareAppMessage(shareObj);
  window.wx.onMenuShareQQ(shareObj);
  window.wx.onMenuShareQZone(shareObj);
  window.wx.onMenuShareWeibo(shareObj);
});
}